package cn.pxs.controller;

import cn.pxs.entity.LargeFile;
import cn.pxs.entity.User;
import cn.pxs.service.LargeFileService;
import cn.pxs.service.UserService;
import cn.pxs.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/api/fore/user")
@Controller
public class ForeUserController {
    @Autowired
    UserService userService;
    @Autowired
    LargeFileService largeFileService;

    @ResponseBody
    @PostMapping("/updatePwd")
    //以键值对 方式
    public Map updatePwd(String pwd){
        Object o =
                SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if("".equals(pwd)){
            return RespJson.pack(404,"没密码啊",null);
        }
        userService.updateForePwd(o,pwd);
        return RespJson.pack(200,"更新密码成功",null);
    }

    @ResponseBody
    @PostMapping("/updateImg")
    //通过 绑定pic 变量 获取文件 / 图片
    public Map updateImg(@RequestParam(value = "pic",required = false)MultipartFile file
                ,LargeFile largeFile){
        Object o =
                SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(file ==  null) return RespJson.pack(404,"图片都没你更新个锤子",null);
        String s = userService.updateForeImg(file, largeFile, o);
        return RespJson.pack(200,"图片成功",s);
    }

    @ResponseBody
    @RequestMapping("/updateInfo")
    //通过键值对 方式
    public Map updateInfo(String nickname,String phone){
        Object o =
                SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(o==null)return RespJson.pack(401,"没登录",null);
        userService.updateInfo(o,nickname,phone);
        return RespJson.pack(200,"更新用户成功",null);
    }


}
